package com.fz.us.admin.dict.dao.impl;

import com.fz.us.admin.base.dao.impl.BaseDaoImpl;
import com.fz.us.admin.dict.dao.DictItemSettingDao;
import com.fz.us.admin.dict.entity.DictItemSetting;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * Dao实现类 - 字典 自定义名称
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-21 。
 * ============================================================================
 */
@Repository
public class DictItemSettingDaoImpl extends BaseDaoImpl<DictItemSetting, String> implements DictItemSettingDao {

    @Override
    public boolean isExistSetting(String keyId, String dictitemId) {
        Assert.hasText(keyId, "keyId is required");
        Assert.hasText(dictitemId, "dictitemId is required");
        String hql = "select count(*) from "+DictItemSetting.class.getName()+" model where model.keyId = :keyId and model.dictId = :dictId";
        return (Long) getSession().createQuery(hql).setParameter("keyId", keyId).setParameter("dictId", dictitemId).uniqueResult() > 0;
    }

    @Override
    public DictItemSetting getSetting(String keyId, String dictitemId) {
        Assert.hasText(keyId, "keyId is required");
        Assert.hasText(dictitemId, "dictitemId is required");
        String hql = "from "+DictItemSetting.class.getName()+" model where model.keyId = :keyId and model.dictId = :dictId";
        return (DictItemSetting) getSession().createQuery(hql).setParameter("keyId", keyId).setParameter("dictId", dictitemId).uniqueResult();
    }

    @Override
    public String getDefinedName(String keyId, String dictitemId) {
        Assert.hasText(keyId, "keyId is required");
        Assert.hasText(dictitemId, "dictitemId is required");
        String hql = "select model.name from "+DictItemSetting.class.getName()+" model where model.keyId = :keyId and model.dictId = :dictId";
        return (String) getSession().createQuery(hql).setParameter("keyId", keyId).setParameter("dictId", dictitemId).uniqueResult();
    }

}
